package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.RegisterView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.LanguageUtils;
import com.alm.token.utils.LogUtil;
import com.alm.token.utils.MyOnclickListener;
import com.alm.token.utils.PopupAdapter;
import com.alm.token.utils.SpUtil;
import com.alm.token.utils.Utils;
import com.alm.token.widget.CommonPopupWindow;
import com.alm.token.widget.Countdownbutton;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnFocusChange;
import okhttp3.ResponseBody;
import retrofit2.Response;

import static com.alm.token.ConStant.county;
import static com.alm.token.ConStant.countyID;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_CODE_VAULE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_INTO_CODE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_NAME_VAULE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_SUCC_CODE;
import static com.alm.token.ui.activity.MnemonicActivity.INTENT_Mnemonic;
import static com.alm.token.ui.activity.MnemonicActivity.INTENT_Mnemonic_INTO_CODE;
import static com.alm.token.ui.activity.MnemonicActivity.INTENT_Mnemonic_RESULT_SUCC_CODE;

public class BlockWalletRegisterActivity extends BaseActivity implements RegisterView<RegisterData>, CommonPopupWindow.ViewInterface {
    @BindView(R.id.edt_inputusername)
    EditText edtInputusername;
    @BindView(R.id.edt_inputpswone)
    EditText edtInputpswone;
    @BindView(R.id.edt_inputpswonce)
    EditText edtInputpswonce;
    @BindView(R.id.edt_inputphone)
    EditText edtInputphone;
    @BindView(R.id.edt_inputcode)
    EditText edtInputcode;
    @BindView(R.id.edt_inputimgcpde)
    EditText edtInputimgcpde;
    @BindView(R.id.tv_getcode)
    Countdownbutton tvGetcode;
    @BindView(R.id.edt_inputinvitecode)
    EditText edtInputinvitecode;
    @BindView(R.id.tv_registersubmit)
    TextView tvRegistersubmit;
    @BindView(R.id.tv_termsofservice)
    TextView tvTermsofservice;
    @BindView(R.id.tv_setphonecode)
    TextView tvSetphonecode;
    @BindView(R.id.tv_clickphonecountry)
    TextView tvClickphonecountry;
    @BindView(R.id.img_getimgcode)
    ImageView imgGetimgcode;

    @BindView(R.id.tv_inputmonic)
    TextView tvInputmonic;
    @BindView(R.id.tv_creatmonic)
    Button tvCreatmonic;


    private String strUserName, strPhone, strCode, strPswone, strPswonce, strinviteCode,strmonic;
    private String[] countys = new String[14];

    @Override
    protected MyPreenter initPreenter() {
//        RetrofitUtil.getInstance().setInstance();
//        MyApp.mOkHttpClient=null;
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_block_wallet_register;
    }

    @Override
    protected void initVariables() {
        for (int i = 0; i < countys.length; i++) {
            countys[i] = getResources().getString(county[i]);
        }

        if (LanguageUtils.getSetIndex() == 1) {
            //中文  name=中国  code=86
            tvSetphonecode.setText("86");
            tvClickphonecountry.setText("中国");
        } else if (LanguageUtils.getSetIndex() == 3) {
            //韩文  name=韩国  code=82
            tvSetphonecode.setText("82");
            tvClickphonecountry.setText("Korea");
        } else if (LanguageUtils.getSetIndex() == 4) {
            //日文 name=日本  code=81
            tvSetphonecode.setText("861");
            tvClickphonecountry.setText("Japan");
        } else {
            //英文 name=美国  code=1
            tvSetphonecode.setText("1");
            tvClickphonecountry.setText("USA");
        }
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.hint_registershow));
        tvRegistersubmit.setOnClickListener(this);
        tvGetcode.setOnClickListener(this);
        tvCreatmonic.setOnClickListener(this);
        tvTermsofservice.setOnClickListener(this);
        tvClickphonecountry.setOnClickListener(this);

        imgGetimgcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
//                RxCaptcha.build()
//                        .backColor(0xffffff)
//                        .codeLength(4)
//                        .fontSize(60)
//                        .lineNumber(2)
//                        .size(200, 70)
//                        .type(RxCaptcha.TYPE.NUMBER)
//                        .into(imgGetimgcode);
            }
        });
    }

    @Override
    protected void loadData() {
        preenter.getcreateCodeData();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.obj != null) {
                Bitmap bm = (Bitmap) msg.obj;
                getcreateCode1(bm);
            }
        }
    };

    public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024]; // 用数据装
        int len = -1;
        while ((len = is.read(buffer)) != -1) {
            outstream.write(buffer, 0, len);
        }
        outstream.close();   // 关闭流一定要记得。

        return outstream.toByteArray();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_clickphonecountry:
//                showDownPop(tvSetphonecode);
                startActivityForResult(FundArealistActivity.class, INTENT_RESULT_INTO_CODE);
                break;

            case R.id.tv_creatmonic:
                //生成助记词
                startActivityForResult(MnemonicActivity.class, INTENT_Mnemonic_INTO_CODE);

                break;

            case R.id.tv_getcode:
                strPhone = edtInputphone.getText().toString();
                if (strPhone.isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn6));
                    edtInputphone.setFocusable(true);
                    edtInputphone.setFocusableInTouchMode(true);
                    edtInputphone.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputphone, 0);
                    break;
                } else if (!Utils.isPhoneNumberValid("+" + Integer.parseInt(tvSetphonecode.getText().toString()) + strPhone,
                        Integer.parseInt(tvSetphonecode.getText().toString()) + "")) {
                    showToast(getString(R.string.profile_hint_warn7));
                    edtInputphone.setFocusable(true);
                    edtInputphone.setFocusableInTouchMode(true);
                    edtInputphone.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputphone, 0);
                    break;
                } else if (EmptyUtils.isEmpty(edtInputimgcpde.getText().toString())) {
                    showToast(getString(R.string.profile_hint_warn111));
                    edtInputimgcpde.setFocusable(true);
                    edtInputimgcpde.setFocusableInTouchMode(true);
                    edtInputimgcpde.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputimgcpde, 0);
                    break;
                }
//                showToast("点击获取验证码");
                tvGetcode.start();
//                Log.e("9999验证码",Integer.parseInt(tvSetphonecode.getText().toString().replace("+",""))+strPhone);
                LogUtil.d("9999验证码:" + "+" + Integer.parseInt(tvSetphonecode.getText().toString()) + strPhone);
                preenter.getSmsCode1Data(strPhone, MyPreenter.register, Integer.parseInt(tvSetphonecode.getText().toString()) + "", edtInputimgcpde.getText().toString());
//                preenter.getSmsCodeData(strPhone);
                break;
            case R.id.tv_registersubmit:
                if (edtInputusername.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn8));
                    edtInputusername.setFocusable(true);
                    edtInputusername.setFocusableInTouchMode(true);
                    edtInputusername.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputusername, 0);
                    break;
                }
                if (edtInputpswone.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn9));
                    edtInputpswone.setFocusable(true);
                    edtInputpswone.setFocusableInTouchMode(true);
                    edtInputpswone.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputpswone, 0);
                    break;
                }
                if (edtInputpswonce.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn10));
                    edtInputpswonce.setFocusable(true);
                    edtInputpswonce.setFocusableInTouchMode(true);
                    edtInputpswonce.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputpswonce, 0);
                    break;
                }
//                if (edtInputphone.getText().toString().isEmpty()) {
//                    showToast(getString(R.string.profile_hint_warn6));
//                    edtInputphone.setFocusable(true);
//                    edtInputphone.setFocusableInTouchMode(true);
//                    edtInputphone.requestFocus();
//                    //显示软键盘
////                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
//                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.showSoftInput(edtInputphone, 0);
//                    break;
//                }
//                if (edtInputcode.getText().toString().isEmpty()) {
//                    showToast(getString(R.string.profile_hint_warn11));
//                    edtInputcode.setFocusable(true);
//                    edtInputcode.setFocusableInTouchMode(true);
//                    edtInputcode.requestFocus();
//                    //显示软键盘
////                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
//                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.showSoftInput(edtInputcode, 0);
//                    break;
//                }

                if (EmptyUtils.isEmpty(edtInputimgcpde.getText().toString())) {
                    showToast(getString(R.string.profile_hint_warn111));
                    edtInputimgcpde.setFocusable(true);
                    edtInputimgcpde.setFocusableInTouchMode(true);
                    edtInputimgcpde.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputimgcpde, 0);
                    break;
                }

//                if (tvInputmonic.getText().toString().isEmpty()) {
//                    showToast(getString(R.string.register_hint_monic_warm));
//                    break;
//                }

                if (edtInputinvitecode.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn12));
                    edtInputinvitecode.setFocusable(true);
                    edtInputinvitecode.setFocusableInTouchMode(true);
                    edtInputinvitecode.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputinvitecode, 0);
                    break;
                }
                strUserName = edtInputusername.getText().toString();
//                strPhone = edtInputphone.getText().toString();
//                strCode = edtInputcode.getText().toString();
                strPswone = edtInputpswone.getText().toString();
                strPswonce = edtInputpswonce.getText().toString();
                strinviteCode = edtInputinvitecode.getText().toString();
                strCode = edtInputimgcpde.getText().toString();
                strmonic=tvInputmonic.getText().toString();

                if (!strPswone.equals(strPswonce)) {
                    showToast(getString(R.string.profile_hint_warn13));
                    break;
                }

                if (!Utils.verifyUserName(strUserName)) {
                    showToast(getString(R.string.profile_hint_warn2));
                    break;
                }
                if (strUserName.length() < 4 || strUserName.length() > 18) {
                    showToast(getString(R.string.profile_hint_warn3));
                    break;
                }
//                if (!(strUserName.substring(0)
//                        + strUserName.substring(strUserName.length() - 1))
//                        .matches("[0-9A-Za-z]*")) {
//                    showToast(getString(R.string.profile_hint_warn4));
//                    break;
//                }
                if (!Utils.verifyUserNameFL(strUserName)) {//[A-Za-z1-9]{2}
                    showToast(getString(R.string.profile_hint_warn4));
                    break;
                }

                if (strPswone.length() < 8 || strPswone.length() > 20) {
                    showToast(getString(R.string.profile_hint_warn33));
                    break;
                }
                if (!Utils.verifyUserNameFL(strPswone)) {//[A-Za-z1-9]{2}
                    showToast(getString(R.string.profile_hint_warn44));
                    break;
                }
                if (!Utils.verifyUserPsw(strPswone)) {
                    showToast(getString(R.string.profile_hint_warn22));
                    break;
                }

//                if (!(strPswone.substring(0)
//                        + strPswone.substring(strPswone.length() - 1))
//                        .matches("[0-9A-Za-z]*")) {
//                    showToast(getString(R.string.profile_hint_warn4));
//                    break;
//                }


//                showToast("提交注册 " + "strUserName= " + strUserName + "  strPhone=" + strPhone + " strCode=" + strCode
//                        + " strPswone=" + strPswone + " strPswonece=" + strPswone + " strinviteCode=" + strinviteCode);


//                preenter.getRegisterData("+"+Integer.parseInt(tvSetphonecode.getText().toString()) + strPhone, strPswone, strCode, strUserName, strinviteCode);
//                preenter.getRegisterData(strPhone, strPswone, strCode, strUserName, strinviteCode);



//                if (tvRegistersubmit.getTag() == null) {
//                    tvRegistersubmit.setTag("register_commit");
//                    preenter.getRegisterData(strPhone, strPswone, strCode, strUserName, strinviteCode);
//                }
                //生成助记词
                startActivityForResult(MnemonicActivity.class, INTENT_Mnemonic_INTO_CODE);
                break;
            case R.id.tv_termsofservice:
//                showToast("点击获取服务条款");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_RESULT_INTO_CODE && resultCode == INTENT_RESULT_SUCC_CODE && data != null) {
            String name = data.getStringExtra(INTENT_RESULT_NAME_VAULE);
            String code = data.getStringExtra(INTENT_RESULT_CODE_VAULE);
            LogUtil.e("name=" + name + "  code=" + code);

            tvSetphonecode.setText(code);
            tvClickphonecountry.setText(name);
        } else if (requestCode == INTENT_Mnemonic_INTO_CODE && resultCode == INTENT_Mnemonic_RESULT_SUCC_CODE && data != null) {
            String monic = data.getStringExtra(INTENT_Mnemonic);
            tvInputmonic.setText(monic);
            strmonic=monic;
//            if (tvRegistersubmit.getTag() == null) {
                tvRegistersubmit.setTag("register_commit");
                preenter.getRegistermailData( strPswone, strCode, strUserName, strinviteCode,strmonic);
//            }

//            String code = data.getStringExtra(INTENT_RESULT_CODE_VAULE);
//            LogUtil.e("name=" + name + "  code=" + code);
//
//            tvSetphonecode.setText(code);
//            tvClickphonecountry.setText(name);
        }
    }

    @OnFocusChange({R.id.edt_inputphone, R.id.edt_inputcode,
            R.id.edt_inputpswone, R.id.edt_inputpswonce})
    void onEdiFocusChange(View view, boolean hasFocus) {
        switch (view.getId()) {
            case R.id.edt_inputphone:

                break;

            case R.id.edt_inputcode:

                break;
            case R.id.edt_inputpswone:
                if (hasFocus && edtInputusername.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn8));
                    edtInputusername.setFocusable(true);
                    edtInputusername.setFocusableInTouchMode(true);
                    edtInputusername.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputusername, 0);
                    break;
                }
                break;
            case R.id.edt_inputpswonce:
                if (hasFocus && edtInputusername.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn8));
                    edtInputusername.setFocusable(true);
                    edtInputusername.setFocusableInTouchMode(true);
                    edtInputusername.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputusername, 0);
                    break;
                }
                if (hasFocus && edtInputpswone.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn9));
                    edtInputpswone.setFocusable(true);
                    edtInputpswone.setFocusableInTouchMode(true);
                    edtInputpswone.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputpswone, 0);
                    break;
                }
                break;
        }
    }

    @Override
    public void getCode(RegisterData registerData) {
//        if(registerData.getStatus()==0){

        if (registerData.getMsg().equalsIgnoreCase(getString(R.string.profile_hint_warn15))) {
            showToast(getString(R.string.profile_hint_warn14));
        } else {
            showToast(registerData.getMsg());
        }

        if (registerData.getStatus() != 0) {
            tvGetcode.setLength(3000);
        }

//        }

    }

    String getCookie(Context context) {
        CookieManager cookieManager = CookieManager.getInstance();
        String cookie = cookieManager.getCookie("cookie");
        if (cookie != null) {
            return cookie;
        } else {
//            cookie= “XXX”;
            cookieManager.setCookie("cookie", cookie);
            return cookie;
        }
    }

    @Override
    public void getcreateCode(Bitmap bitmap) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] data = new byte[0];
                try {

                    HashSet<String> preferences = (HashSet) MyApp.context.getSharedPreferences("config",
                            MyApp.context.MODE_PRIVATE).getStringSet("cookie", null);

                    StringBuffer stringBuffer = new StringBuffer();

                    if (preferences != null) {
                        for (String cookie : preferences) {
                            if (cookie.indexOf("lang=") == -1) {
                                cookie = cookie + "; " + SpUtil.getString(MyApp.context, ConStant.COOKIELANGAUE, "" + LanguageUtils.COOKIE_CHINESE);
                            }
                            stringBuffer.append(cookie);
                            LogUtil.v("getCookie=" + stringBuffer.toString()); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
                        }
                    } else {
                        LogUtil.v("getCookie=" + null); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
                    }

                    URL url = new URL(ConStant.HOST + "/app/API/createCode");
                    HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
                    httpUrlConn.setRequestProperty("cookie", stringBuffer.toString());
                    httpUrlConn.setRequestMethod("GET");
                    httpUrlConn.setConnectTimeout(10 * 1000);
                    int code = httpUrlConn.getResponseCode();
                    if (code == 200) {
                        InputStream inputStream = httpUrlConn.getInputStream();
//                        System.out.println("=====================服务器返回的信息：：" + result);
                        data = getBytesFromInputStream(inputStream);
                    }

                    LogUtil.e("getCookie=" + stringBuffer.toString());

//                    data = getBytesFromInputStream(url.openStream());

//                        data = getBytesFromInputStream(new URL(ConStant.HOST + "/app/API/createCode").openStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
                LogUtil.e("bm=" + EmptyUtils.isNotEmpty(bm));
                Message message = handler.obtainMessage();
                message.obj = bm;
                handler.sendMessage(message);

//                getcreateCode(bm);
            }
        }).start();


//        if (EmptyUtils.isEmpty(bitmap)) {
////            showToast(getString(R.string.common_loaderrritry));
//
//        } else {
//            imgGetimgcode.setImageBitmap(bitmap);
//        }


//        imgGetimgcode.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                loadData();
////                RxCaptcha.build()
////                        .backColor(0xffffff)
////                        .codeLength(4)
////                        .fontSize(60)
////                        .lineNumber(2)
////                        .size(200, 70)
////                        .type(RxCaptcha.TYPE.NUMBER)
////                        .into(imgGetimgcode);
//            }
//        });

    }

    public void getcreateCode1(Bitmap bitmap) {
        imgGetimgcode.setImageBitmap(bitmap);
    }

    @Override
    public void getData(RegisterData registerData) {
        tvRegistersubmit.setTag(null);
        showToast(registerData.getMsg());
        if (registerData.getStatus() == 0)
            finish();
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
            tvRegistersubmit.setTag(null);
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
//        showToast(error);
        tvRegistersubmit.setTag(null);
        showToast(getString(R.string.profile_hint_warn16));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog(getString(R.string.profile_hint_warn17));
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    private CommonPopupWindow popupWindow;

    //向下弹出
    public void showDownPop(View view) {
        if (countrys.size() == 0) {
            for (int i = 0; i < countys.length; i++) {
                countrys.add(countys[i]);
                countryMap.put(countys[i], countyID[i]);
            }
        }
        if (popupWindow != null && popupWindow.isShowing()) return;
        popupWindow = new CommonPopupWindow.Builder(this)
                .setView(R.layout.popup_down)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setAnimationStyle(R.style.AnimDown)
                .setViewOnclickListener(this)
                .setOutsideTouchable(true)
                .create();
        popupWindow.showAsDropDown(view);
        //得到button的左上角坐标
//        int[] positions = new int[2];
//        view.getLocationOnScreen(positions);
//        popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, 0, positions[1] + view.getHeight());
    }

    public List<String> countrys = new ArrayList<>();
    public Map<String, String> countryMap = new HashMap<>();

    @Override
    public void getChildView(View view, int layoutResId) {
        //获得PopupWindow布局里的View
        switch (layoutResId) {
            case R.layout.popup_down:
                RecyclerView recycle_view = (RecyclerView) view.findViewById(R.id.recycle_view);
                recycle_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//                recycle_view.setLayoutManager(new GridLayoutManager(this, 3));
                PopupAdapter mAdapter = new PopupAdapter(this, countrys);
                recycle_view.setAdapter(mAdapter);
                mAdapter.setOnItemClickListener(new MyOnclickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
//                        showToast("position is " + position);
                        tvSetphonecode.setText(countryMap.get(countrys.get(position)));
                        tvClickphonecountry.setText(countrys.get(position));
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                    }
                });
                break;
        }
    }
}
